﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using JKF.Entities.Model;
using JKF.Substructure;

namespace JKF.DataModule.SqlServerDAL
{
    public partial class dalCALL_LOG:IDAL.IdalCALL_LOG
    {
        // 定义要获取哪些字段  NAME, ID, SEX, PHONE, CALL_DATE, CONTENT, MEMO, CARD_NO, LEVEL_NAME 
        private const string FieldShow = "NAME, ID, SEX, PHONE, CALL_DATE, CONTENT, MEMO, CARD_NO, LEVEL_NAME ";
        private const string FieldOrder = "NAME,PHONE,CALL_DATE,CARD_NO";
        private const string FieldKey = "ID"; 
        /// <summary>
        ///  获取通话记录列表
        /// </summary>
        /// <param name="userinfo">当前用户</param>
        /// <param name="CARD_NO">卡号</param>
        /// <param name="NAME">姓名</param>
        /// <param name="PHONE">手机</param>
        /// <param name="LEVEL_NAME">级别名称</param>
        /// <param name="DATE_START">通话日期开始</param>
        /// <param name="DATE_END">通话日期结束</param>
        /// <param name="pageCurrent">当前页码</param>
        /// <param name="pageSize">一页显示多少</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录数</param>
        /// <returns>返回DataTable结果集</returns>
        public static DataTable GetCALL_LOGSDAL(SYS_USERinfo userinfo, string CARD_NO, string NAME, string PHONE, string LEVEL_NAME, string DATE_START, string DATE_END, string CUSTOMER_KEY, string ownShop, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            var sql = new StringBuilder(); 
              //其他店铺只能看到自己创建和总店创建的通话记录
                sql.AppendFormat(string.Format(@" status='1' and  shop_code in(  select shop_code from  sys_user_shop where user_key='{0}')", userinfo.USER_KEY.ToString())); 
            // 下面组装查询条件 

            if (!string.IsNullOrEmpty(CARD_NO))
            {
                sql.AppendFormat(" AND CARD_NO  LIKE '%{0}%'", CARD_NO);
            }
            if (!string.IsNullOrEmpty(NAME))
            {
                sql.AppendFormat(" AND NAME  LIKE '%{0}%'", NAME);
            }
            if (!string.IsNullOrEmpty(PHONE))
            {
                sql.AppendFormat(" AND PHONE LIKE '%{0}%'", PHONE);
            }
            if (!string.IsNullOrEmpty(LEVEL_NAME))
            {
                sql.AppendFormat(" AND LEVEL_NAME  LIKE '%{0}%'", LEVEL_NAME);
            }
            if (!string.IsNullOrEmpty(DATE_START))
            {
                sql.AppendFormat(" and CALL_DATE >='{0}'", DATE_START);
            }
            if (!string.IsNullOrEmpty(DATE_END))
            {
                sql.AppendFormat(" and CALL_DATE <='{0}'", DATE_END);
            }
            if (!string.IsNullOrEmpty(CUSTOMER_KEY))
            {
                sql.AppendFormat(" and CUSTOMER_KEY ='{0}'", CUSTOMER_KEY);
            }
            if (!string.IsNullOrEmpty(ownShop))
            {
                sql.AppendFormat(" and Shop_code in('{0}')", string.Join("','", ownShop.Split(',')));
            }
            // 下面调用此方法去查询数据库
            return DataPage.GetPageInfo("dbo.V_CALL_LOG_CUSTOMER", FieldKey, pageCurrent, pageSize, FieldShow, FieldOrder, sql.ToString(), ref pageCount, ref rowCount).Tables[0];

        }

        /// <summary>
        /// 事务执行SQL语句
        /// </summary>
        /// <returns></returns>
        public static bool TransationSql(string sql)
        {
            //采用事务提交
            using (TransactionHR tHR = DB.BeginTransactionHR())
            {
                try
                {
                    int result = tHR.ExecuteNonQuery(sql);

                    if (result > 0)
                    {
                        tHR.Commit();
                        return true;
                    }
                    else
                    {
                        tHR.Rollback();
                        return false;
                    }
                }
                catch (Exception ex)
                {
                    tHR.Rollback();
                    //return false;
                    throw ex;

                }
            }
        }
    }
}
